<?php
declare(strict_types=1);

namespace App\Controller\admin;

use App\Annotation\AuthUser;
use App\Controller\AbstractController;
use App\Request\Admin\ReviewArticleRequest;
use App\Request\Admin\ReviewControversialRequest;
use App\Resource\Admin\ArticleResource;
use App\Resource\Admin\SimpleArticleResource;
use App\Service\Admin\ArticleService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PatchMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;

#[Controller(prefix: '/api/admin/articles')]
class ArticleController extends AbstractController
{
    #[Inject]
    private ArticleService $articleService;

    #[GetMapping('')]
    public function getReviewArticles(RequestInterface $request)
    {
        $page = $request->query('page', 1);
        $perPage = $request->query('perPage', 15);

        $articles = $this->articleService->getReviewArticles($page, $perPage);

        return SimpleArticleResource::collection($articles);
    }


    #[GetMapping('{id}')]
    public function getArticleDetail(int $id)
    {
        $article = $this->articleService->getArticleDetail($id);

        return new ArticleResource($article);
    }

    #[PostMapping('review')]
    public function review(ReviewArticleRequest $request, #[AuthUser] ?int $authUserId)
    {
        $data = $request->validated();

        $this->articleService->review($data, $authUserId);

        return [];
    }

    #[PatchMapping('{id}/controversial')]
    public function reviewControversial(int $id, #[AuthUser] ?int $authUserId, ReviewControversialRequest $request)
    {
        $data = $request->validated();

        $this->articleService->reviewControversial($id, $authUserId, $data['approved']);

        return [];
    }
}